<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>测试发布订阅-发布</title>
  </head>
  <script src="./手写发布订阅.js"></script>
  <body>
    <button id="btn">发布消息</button>
    <script>
      const $event = new EventEmitter();
      btn.onclick = function () {
        $event.emit("sendZs", {
          name: "张三",
          age: 18,
        });
        $event.emit("sendLs", {
          name: "李四",
          age: 2,
        });
        $event.emit("sendWw", {
          name: "王五",
          age: 48,
        });
      };

      const getWw = (val) => {
        console.log("订阅了王五的消息", val);
      };

      $event.on("sendLs", (val) => {
        console.log("订阅到的消息", val);
      });
      $event.on("sendWw", getWw);
      setTimeout(() => {
        $event.off("sendWw", getWw);
      }, 2000);
      // $event.once("send", (val) => {
      //   console.log("订阅到的一次消息", val);
      // });
    </script>
  </body>
</html>
